package jblip.gui;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

/**
 * Configuration of JBlip GUI.
 * 
 * <p>
 * This class handles the configuration for the GUI interface, based on a
 * provided configuration file.
 * </p>
 * 
 * @author Krzysztof Sroka
 */
public class Configuration {
  private static final Properties DEFAULTS;

  static {
    DEFAULTS = new Properties();
    DEFAULTS.setProperty("jblip.spyDashboards", "false");
    DEFAULTS.setProperty("jblip.channels.showTags", "true");
    DEFAULTS.setProperty("jblip.channels.showShpere", "false");
    DEFAULTS.setProperty("jblip.channels.showSelf", "false");
    DEFAULTS.setProperty("jblip.channels.showNotices", "false");
    DEFAULTS.setProperty("jblip.talk.useTabs", "true");
    DEFAULTS.setProperty("jblip.talk.showBackground", "false");
    DEFAULTS.setProperty("jblip.talk.showMedia", "true");
    DEFAULTS.setProperty("jblip.ui.view.width", "600");
  }

  public static String getDefaultJBlipDirectory() {
    final String DIR_UNIX =
        System.getProperty("user.home") + File.separator + ".jblip";
    final String DIR_WIN =
        System.getProperty("user.home") + File.separator + "JBlip";
    final String os_name = System.getProperty("os.name").toLowerCase();
    System.err.println("OS used: " + os_name);
    if (os_name.contains("windows")) {
      return DIR_WIN;
    } else {
      return DIR_UNIX;
    }
  }

  private final Properties properties;

  private final File props_file;

  private boolean store_passwd;

  public Configuration(final File properties_file) throws FileNotFoundException,
      IOException {
    this.properties = new Properties(DEFAULTS);
    if (!properties_file.exists()) {
      properties_file.createNewFile();
    }
    properties.load(new FileReader(properties_file));
    this.store_passwd = properties.containsKey("jblip.pass");
    this.props_file = properties_file;
  }

  void store() throws IOException {
    System.err.println("Storing configuration to " + props_file.getPath());
    FileWriter props_writer = new FileWriter(props_file);
    if (!store_passwd) {
      properties.remove("jblip.pass");
    }
    properties.store(props_writer, "Properties for the JBlip application");
  }

  public boolean hasLogin() {
    return getLogin() != null;
  }

  public String getLogin() {
    return properties.getProperty("jblip.user");
  }

  public boolean hasPassword() {
    return getPassword() != null;
  }

  public String getPassword() {
    return properties.getProperty("jblip.pass");
  }
  
  public void setStorePassword(final boolean store) {
    this.store_passwd  = store;
  }

  public void setCredentials(final String user, final String pass) {
    properties.setProperty("jblip.user", user);
    properties.setProperty("jblip.pass", pass);
  }

  public void unsetCredentials() {
    properties.remove("jblip.user");
    properties.remove("jblip.pass");
  }
  
  public int getViewWidth() {
    return Integer.parseInt(properties.getProperty("jblip.ui.view.width"));
  }
  
  public void setViewWidth(final int width) {
    if (width > 0) {
      properties.setProperty("jblip.ui.view.width", String.valueOf(width));
    } else {
      System.err.println("Non-positive width given");
    }
  }
}
